const path = require('path')
const {
    spawn
} = require('child_process')
const NODE_ENV = process.env.NODE_ENV
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')
const rules = require('./webpack/rule')
const port = process.env.PORT || 1212;

module.exports = {
    devServer: {
        port,
        compress: true,
        noInfo: true,
        stats: 'errors-only',
        inline: true,
        lazy: false,
        hot: true,
        headers: {
            'Access-Control-Allow-Origin': '*'
        },
        contentBase: path.join(__dirname, 'public'),
        watchOptions: {
            aggregateTimeout: 300,
            ignored: /node_modules/,
            poll: 100
        },
        historyApiFallback: {
            verbose: true,
            disableDotRule: false
        },
        before() {
            if (process.env.START_HOT) {
                process.env.WEBPACK_DEV_SERVER_URL = `http://localhost:${port}`
                console.log('Starting Main Process...');
                spawn('npm', ['run', 'start-main-dev'], {
                        shell: true,
                        env: process.env,
                        stdio: 'inherit'
                    })
                    .on('close', code => process.exit(code))
                    .on('error', spawnError => console.error(spawnError));
            }
        }
    },
    mode: NODE_ENV,
    node: {
        setImmediate: false,
        process: 'mock',
        dgram: 'empty',
        fs: 'empty',
        net: 'empty',
        tls: 'empty',
        child_process: 'empty'
    },
    resolve: {
        alias: {
            '@': path.join(__dirname, 'src')
        },
        extensions: ['.json', '.js', '.jsx', '.ts', '.tsx'],
        modules: ["node_modules"]
    },
    entry: path.join(__dirname, "src/main.tsx"),
    devtool: 'inline-source-map',
    target: 'electron-renderer',
    output: {
        path: path.join(__dirname, 'dist'),
        filename: 'js/[name].js',
        publicPath: '/',
        chunkFilename: 'js/[name].js'
    },
    module: {
        rules
    },
    externals: {
        Blockly: 'blockly',
        hterm: 'hterm',
        electronUploader: 'electron-updater',
        SerialPort: 'serialport',
        "node-hid": 'commonjs node-hid'
    },
    optimization: {
        splitChunks: {
            cacheGroups: {
                vendors: {
                    name: 'chunk-vendors',
                    test: /[\\/]node_modules[\\/]/,
                    priority: -10,
                    chunks: 'initial'
                },
                common: {
                    name: 'chunk-common',
                    minChunks: 2,
                    priority: -20,
                    chunks: 'initial',
                    reuseExistingChunk: true
                }
            }
        }
    },
    plugins: [
        new CaseSensitivePathsPlugin(),
        new HtmlWebpackPlugin({
            title: 'Raspberry Pi 机械手',
            template: 'public/index.html',
            filename: 'index.html'
        })
    ]
}